#include <rclcpp/rclcpp.hpp>

class ParamServerNode: public rclcpp::Node
{
public:
//true 允许你的节点在运行时动态地接收、创建和管理那些没有在代码中预先声明的参数
//set_parameter 可以删除 
//声明
    ParamServerNode():Node("param_server_node",
        rclcpp::NodeOptions().allow_undeclared_parameters(true))
    {
        RCLCPP_INFO(this->get_logger(),"ParamServerchu创建");
    }
public:    
    void add_param()
    {
        RCLCPP_INFO(this->get_logger(),"----------------增-----------------");
        this->declare_parameter("car_name","tiger");
        this->declare_parameter("wheels",5);
        this->declare_parameter("width",1.55);
        //只有allow_undeclared_parameters(true) 才可以用于设置
        this->set_parameter(rclcpp::Parameter("height",2.00));
    }
    void delete_param()
    {
        RCLCPP_INFO(this->get_logger(),"----------------删-----------------");
        auto hh="height";
        this->undeclare_parameter(hh);
        RCLCPP_INFO(this->get_logger(),"删除后还包含%s么？%d",hh,this->has_parameter(hh));
    }
    void update_param()
    {
        RCLCPP_INFO(this->get_logger(),"----------------改-----------------");
        //不能删除声明的参数
        this->set_parameter(rclcpp::Parameter("width",1.75));
        RCLCPP_INFO(this->get_logger(),"width=%.2f",this->get_parameter("width").as_double());
    }
    void get_param()
    {
        RCLCPP_INFO(this->get_logger(),"----------------查-----------------");
        std::string car_name="car_name";
        rclcpp::Parameter p1=this->get_parameter(car_name);
        RCLCPP_INFO(this->get_logger(),"key = %s,value=%s",p1.get_name().c_str(),p1.as_string().c_str());
        //
        std::vector<rclcpp::Parameter> params = this->get_parameters({"car_name", "wheels", "width"});
        for(rclcpp::Parameter param:params)
        {
            RCLCPP_INFO(this->get_logger(),"%s = %s",param.get_name().c_str(),
            param.value_to_string().c_str());
        }
        RCLCPP_INFO(this->get_logger(),"是否包含car_name %d",this->has_parameter("car_name"));
        RCLCPP_INFO(this->get_logger(),"是否包含height %d",this->has_parameter("height"));
    }
};
int main(int argc, char ** argv)
{
    rclcpp::init(argc,argv);
    std::shared_ptr<ParamServerNode>  server_node=std::make_shared<ParamServerNode>();
    server_node->add_param();
    server_node->get_param();
    server_node->update_param();
    server_node->delete_param();
    rclcpp::spin(server_node);
    rclcpp::shutdown();
    return 0;
}